package com.example.homeworkdev.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.homeworkdev.R;
import com.example.homeworkdev.entity.Message;

import java.util.List;

public class MessageInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_SENT = 1;
    private static final int VIEW_TYPE_RECEIVED = 2;
    private List<Message> messageList;

    public MessageInfoAdapter(List<Message> messageList) {
        this.messageList = messageList;
    }

    @Override
    public int getItemViewType(int position) {
        return messageList.get(position).isSend() ? VIEW_TYPE_SENT : VIEW_TYPE_RECEIVED;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == VIEW_TYPE_SENT) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message, parent, false);
            return new SentMessageViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message, parent, false);
            return new ReceivedMessageViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Message message = messageList.get(position);
        if (holder instanceof SentMessageViewHolder) {
            SentMessageViewHolder sentHolder = (SentMessageViewHolder) holder;
            sentHolder.textViewMessageSent.setText(message.getMesssage());
            sentHolder.textViewTimeSent.setText(message.getTime());
            sentHolder.avatarSent.setImageResource(message.getImg());
        } else if (holder instanceof ReceivedMessageViewHolder) {
            ReceivedMessageViewHolder receivedHolder = (ReceivedMessageViewHolder) holder;
            receivedHolder.textViewMessageReceived.setText(message.getMesssage());
            receivedHolder.textViewTimeReceived.setText(message.getTime());
            receivedHolder.avatarReceived.setImageResource(message.getImg());
        }
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    // 自己消息的ViewHolder
    static class SentMessageViewHolder extends RecyclerView.ViewHolder {
        TextView textViewMessageSent;
        TextView textViewTimeSent;
        ImageView avatarSent;

        SentMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewMessageSent = itemView.findViewById(R.id.textViewMessageSent);
            textViewTimeSent = itemView.findViewById(R.id.textViewTimeSent);
            avatarSent = itemView.findViewById(R.id.avatarSent);
            // 隐藏对方消息布局
            itemView.findViewById(R.id.layoutMessageReceived).setVisibility(View.GONE);
        }
    }

    // 对方消息的ViewHolder
    static class ReceivedMessageViewHolder extends RecyclerView.ViewHolder {
        TextView textViewMessageReceived;
        TextView textViewTimeReceived;
        ImageView avatarReceived;

        ReceivedMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewMessageReceived = itemView.findViewById(R.id.textViewMessageReceived);
            textViewTimeReceived = itemView.findViewById(R.id.textViewTimeReceived);
            avatarReceived = itemView.findViewById(R.id.avatarReceived);
            // 隐藏自己消息布局
            itemView.findViewById(R.id.layoutMessageSent).setVisibility(View.GONE);
        }
    }
}
